package com.lindroy.androidplayer.ui.collection

import android.widget.ImageView
import androidx.activity.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseActivity
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.*
import com.lindroy.androidplayer.logic.model.CollectArticleResp
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import com.orhanobut.logger.Logger
import com.youngfeng.snake.annotations.EnableDragToClose
import kotlinx.android.synthetic.main.activity_collection.*


/**
 * @author Lin
 * @date 2021/4/22
 * @function 我的收藏文章列表
 * @Description
 */
@EnableDragToClose
class CollectionActivity(override val layoutId: Int = R.layout.activity_collection) :
    BaseActivity() {

    private val collectViewModel: CollectViewModel by viewModels()

    private val adapter: BaseSimpleAdapter<CollectArticleResp> by lazy {
        BaseSimpleAdapter(R.layout.collected_article_item) { holder, item ->
            holder.setText(R.id.tvTitle, item.title)
                .setText(R.id.tvDate, item.niceDate)
                .setText(R.id.tvAuthor, item.author)
                .setText(R.id.tvChapter, item.chapterName)
            holder.getView<ImageView>(R.id.ivCollect).also {
                it.setImageResource(R.drawable.ic_collection_selected)
                it.setOnClickListener {
                    collectViewModel.unCollectArticle(item.id, item.originId)
                }
            }
            holder.itemView.setOnClickListener {
                ContentActivity.start(this,item.id,item.link,item.title,true)
            }
        }
    }

    override fun initView() {
        initToolBar(R.string.nav_my_collection)
        refreshHeader.initColor()
        rvArticle.setLineDivider()
        rvArticle.adapter = adapter
    }

    override fun initData() {
        stateLayout.showLoading()
        addLoadingObserve(collectViewModel)
        collectViewModel.getCollectedArticles(true)
    }

    override fun initListener() {

        refreshLayout.setOnRefreshListener {
            collectViewModel.getCollectedArticles(true)
        }
        refreshLayout.setOnLoadMoreListener {
            collectViewModel.getCollectedArticles(false)
        }
        collectViewModel.collectedArticlesState.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        collectViewModel.collectState.observe(this) {
            it.success.yes {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect,it.originId)
            }
        }
        eventViewModel.collectEvent.observeInActivity(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.id
            }
            Logger.d("Collected，id=${collectBus.id}，collect=${collectBus.collect}")
            if (index >= 0) {
                adapter.removeAt(index)
            }
            if (adapter.data.isEmpty()) {
                stateLayout.showEmpty()
            }
        }
    }

    override fun initClick() {

    }
}